package cooperate

import (
	"gddgame.cc/galaxy/common/namespaces/cooperate/structure"
	"gddgame.cc/galaxy/utils/serialize"
)

type TaskBuilder struct {
	execute *LogicExecute
	mapNoId string

	from  *structure.BaseData
	task  structure.TaskEvent
	time  int64
	param structure.ParamSlice

	b    []byte
	slot serialize.Slot
}

func (logic *LogicExecute) TaskBuilder() *TaskBuilder {
	return &TaskBuilder{
		execute: logic,
	}
}

func (builder *TaskBuilder) BindMapNoId(mapNoId string) *TaskBuilder {
	builder.mapNoId = mapNoId
	return builder
}

func (builder *TaskBuilder) Content() *TaskContent {
	content := TaskContent{
		From:    structure.NewBaseDataFrom(builder.from),
		Task:    builder.task,
		Content: builder.b,
	}
	return &content
}

func (builder *TaskBuilder) From(from *structure.BaseData) *TaskBuilder {
	builder.from = from
	return builder
}
func (builder *TaskBuilder) Bind(task structure.TaskEvent, b []byte) *TaskBuilder {
	builder.task = task
	builder.b = b
	return builder
}
func (builder *TaskBuilder) One(task structure.TaskEvent, data serialize.IStruct) *TaskBuilder {
	slot := serialize.GetSlot()
	builder.slot = slot
	builder.task = task
	_ = slot.Set(data)
	builder.b = slot.Bytes()
	return builder
}

func (builder *TaskBuilder) Task(time int64, param structure.ParamSlice) {
	builder.time = time
	builder.param = param
	builder.execute.PushTask(builder)
}

func (builder *TaskBuilder) executor(conn structure.SafeConn) {
	builder.execute.execute.AddTask(conn, builder.mapNoId, builder.from, builder.task, builder.b, builder.time, builder.param, nil)
	if builder.slot != nil {
		conn.AutoRelease(builder.slot)
		builder.slot = nil
	}
	builder.execute = nil
	builder.from = nil
	builder.param = nil
}
